﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using TecBaseObjects.Objects;
using TpNotiz.Controller;
using TpNotiz.Interfaces;

namespace TpNotiz.Forms {

    public partial class TecNotizMain : Form, ITecForm {

        #region Members

        private TecUserObj currentUser;
        public Timer myTimer;

        private MainController mainController;

        #endregion

        #region Constructor

        public TecNotizMain() {
            this.mainController = new MainController(this);
            InitializeComponent();
            this.mainController.LoadModels();
            this.mainController.SetTimer();
            SetFormDataSources();
            SetControls();
        }

        #endregion

        #region Properties

        public TecUserObj CurrentUser {
            get { return this.currentUser; }
            set { this.currentUser = value; }
        }

        public List<TecNotizObj> MyModel {
            get;
            set;
        }

        public List<TecUserObj> MyUserModel {
            get;
            set;
        }

        #endregion

        #region Methods

        private void SetControls() {
            int selectIndex = 0;
            this.comboBox1.SelectedValue = "UserID";
            this.comboBox1.DisplayMember = "LoginName";
            foreach (TecUserObj u1 in this.MyUserModel) {
                if (u1.LoginName == this.CurrentUser.LoginName.Trim()) {
                    this.comboBox1.SelectedIndex = selectIndex;
                    break;
                }
                selectIndex++;
            }
        }

        public void SetFormDataSources() {
            this.bindingSource1.DataSource = MyModel;
            this.bindingSourceUser.DataSource = MyUserModel;
        }

        #endregion

        #region FormEvents

        private void Close(object sender, FormClosedEventArgs e) {
            this.mainController.Close(sender, e);
        }

        private void NewNotiz(object sender, EventArgs e) {
            mainController.CreateNewNotiz(this.dataGridView1, e);
        }
        private void OpenNotiz(object sender, DataGridViewCellEventArgs e) {
            mainController.OpenDetailForm(sender, e);
        }

        private void UserChanged(object sender, EventArgs e) {
            mainController.SelectedIndexChanged(sender, e);
        }

        

        #endregion

    }
}
